﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading.Tasks;
using Acme.BookStore2.Permissions;
using Microsoft.AspNetCore.Authorization;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Services;
using Volo.Abp.ObjectExtending;

namespace Acme.BookStore2
{
    [Authorize(BookStore2Permissions.VehicleMaintenanceRecord.Default)]
    public class VehicleMaintenanceRecordAppService : ApplicationService, IVehicleMaintenanceRecordAppService
    {
        private readonly IVehicleMaintenanceRecordRepository _vehicleMaintenanceRecordRepository;
        private readonly VehicleMaintenanceRecordManager _vehicleMaintenanceRecordManager;

        public VehicleMaintenanceRecordAppService(
            IVehicleMaintenanceRecordRepository vehicleMaintenanceRecordRepository, 
            VehicleMaintenanceRecordManager vehicleMaintenanceRecordManager)
        {
            _vehicleMaintenanceRecordRepository = vehicleMaintenanceRecordRepository;
            _vehicleMaintenanceRecordManager = vehicleMaintenanceRecordManager;
        }

        public async Task<VehicleMaintenanceRecordDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<VehicleMaintenanceRecord, VehicleMaintenanceRecordDto>(await _vehicleMaintenanceRecordRepository.GetAsync(id, false));
        }

        public async Task<PagedResultDto<VehicleMaintenanceRecordDto>> GetPagedListAsync(GetVehicleMaintenanceRecordsInput input)
        {
            var count = await _vehicleMaintenanceRecordRepository.GetCountAsync(input.VehicleId, input.StartDateBegin, input.StartDateEnd);
            var list = await _vehicleMaintenanceRecordRepository.GetPagedListAsync(input.SkipCount, input.MaxResultCount,
                input.Sorting, input.VehicleId,input.StartDateBegin, input.StartDateEnd);

            return new PagedResultDto<VehicleMaintenanceRecordDto>(
                count, 
                ObjectMapper.Map<List<VehicleMaintenanceRecord>, List<VehicleMaintenanceRecordDto>>(list));
        }

        [Authorize(BookStore2Permissions.VehicleMaintenanceRecord.Create)]
        public async Task<VehicleMaintenanceRecordDto> CreateAsync(VehicleMaintenanceRecordCreateDto input)
        {
            var vehicleMaintenanceRecord = new VehicleMaintenanceRecord(
                GuidGenerator.Create(),
                input.VehicleId,
                CurrentTenant.Id);

            UpdateVehicleMaintenanceRecord(vehicleMaintenanceRecord, input);
            input.MapExtraPropertiesTo(vehicleMaintenanceRecord);

            await _vehicleMaintenanceRecordRepository.InsertAsync(vehicleMaintenanceRecord);
            await CurrentUnitOfWork.SaveChangesAsync();

            return ObjectMapper.Map<VehicleMaintenanceRecord, VehicleMaintenanceRecordDto>(vehicleMaintenanceRecord);
        }

        [Authorize(BookStore2Permissions.VehicleMaintenanceRecord.Update)]
        public async Task<VehicleMaintenanceRecordDto> UpdateAsync(Guid id, VehicleMaintenanceRecordUpdateDto input)
        {
            var vehicleMaintenanceRecord = await _vehicleMaintenanceRecordRepository.GetAsync(id);
            vehicleMaintenanceRecord.ConcurrencyStamp = input.ConcurrencyStamp;

            UpdateVehicleMaintenanceRecord(vehicleMaintenanceRecord, input);
            input.MapExtraPropertiesTo(vehicleMaintenanceRecord);

            await _vehicleMaintenanceRecordRepository.UpdateAsync(vehicleMaintenanceRecord);
            await CurrentUnitOfWork.SaveChangesAsync();

            return ObjectMapper.Map<VehicleMaintenanceRecord, VehicleMaintenanceRecordDto>(vehicleMaintenanceRecord);
        }

        [Authorize(BookStore2Permissions.VehicleMaintenanceRecord.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            var vehicleMaintenanceRecord = await _vehicleMaintenanceRecordRepository.FindAsync(id);
            if (vehicleMaintenanceRecord == null)
            {
                return;
            }

            await _vehicleMaintenanceRecordRepository.DeleteAsync(vehicleMaintenanceRecord);
        }

        [Authorize(BookStore2Permissions.VehicleMaintenanceRecord.Export)]
        public async Task ExportAsync(GetVehicleMaintenanceRecordsInput input)
        {
        }

        private void UpdateVehicleMaintenanceRecord(VehicleMaintenanceRecord vehicleMaintenanceRecord, VehicleMaintenanceRecordCreateOrUpdateDtoBase input)
        { 
            vehicleMaintenanceRecord.StartDate = input.StartDate;
            vehicleMaintenanceRecord.EndDate = input.EndDate;
            vehicleMaintenanceRecord.Location = input.Location;
            vehicleMaintenanceRecord.Cost = input.Cost;
            vehicleMaintenanceRecord.Technician = input.Technician;
            vehicleMaintenanceRecord.Detail = input.Detail;
            vehicleMaintenanceRecord.Remark = input.Remark;
            
            ObjectMapper.Map(input.AttachFiles, vehicleMaintenanceRecord.AttachFiles);
        }
    }
}